package com.zyd.shiro.business.service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Expression;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import javax.persistence.criteria.Selection;

import com.zyd.shiro.business.vo.UserVO;
import com.zyd.shiro.persistence.beans.Order;
import com.zyd.shiro.persistence.dao.MallOrderDao;
import com.zyd.shiro.util.BigDecimalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;


import com.zyd.shiro.persistence.dao.MallUserDao;
import com.zyd.shiro.persistence.beans.User;

/**
 * 服务层
 *
 * @author RookieWZW
 */
@Service
public class MallUserService {

    @Autowired
    private MallUserDao userDao;

    @Autowired
    private MallOrderDao mallOrderDao;


    public List<UserVO> findAll() {

        List<User> userList = userDao.findAll();

        return UserToUserVO(userList);
    }

    private Specification<User> where(Map searchMap) {

        return new Specification<User>() {

            @Override
            public Predicate toPredicate(Root<User> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                List<Predicate> predicateList = new ArrayList<Predicate>();

                // 用户昵称
                if (searchMap.get("nickName") != null && !"".equals(searchMap.get("nickName"))) {
                    predicateList.add(cb.like(root.get("nickName").as(String.class), "%" + (String) searchMap.get("nickName") + "%"));
                }
                //性别
                if (searchMap.get("gender") != null && !"".equals(searchMap.get("gender"))) {
                    predicateList.add(cb.equal(root.get("gender").as(Integer.class), searchMap.get("gender")));
                }

                return cb.and(predicateList.toArray(new Predicate[predicateList.size()]));

            }
        };

    }

    public List<UserVO> findSearch(Map whereMap) {
        Specification<User> specification = where(whereMap);

        List<User> userList = userDao.findAll(specification);

        return UserToUserVO(userList);

    }

    public User findOne(Integer id) {
        return userDao.findOne(id);
    }


    public void delete(Integer id) {
        userDao.delete(id);
    }


    private List<UserVO> UserToUserVO(List<User> userList) {
        List<UserVO> userVOList = new ArrayList<>();

        for (User user : userList) {
            UserVO userVO = new UserVO();
            userVO.setId(user.getId());
            userVO.setAvatarUrl(user.getAvatarUrl());
            userVO.setCity(user.getCity());
            userVO.setCountry(user.getCountry());
            userVO.setCreate_time(user.getCreate_time());
            userVO.setGender(user.getGender());
            userVO.setNickName(user.getNickName());
            userVO.setProvince(user.getProvince());

            List<Order> orderList = mallOrderDao.getAllByUser_id(user.getId());

            BigDecimal bigDecimal = new BigDecimal("0");
            for (Order order : orderList) {
                bigDecimal = BigDecimalUtil.add(bigDecimal.doubleValue(), order.getTotal_price());
            }
            userVO.setTotal_price(bigDecimal.doubleValue());

            userVOList.add(userVO);

        }

        return userVOList;
    }

}
